<?php
	header("Content-type:text/xml");
	ini_set('max_execution_time', 7000);
	//require_once('../../common/config.php'); 
	print("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
        require_once '../../../../conexion.php';
?>
<?php
	//$link = mysql_pconnect($mysql_host, $mysql_user, $mysql_pasw);
	//$db = mysql_select_db ($mysql_db);
        
        
	if (!isset($_GET["pos"])) $_GET["pos"]=0;

	//Create database and table if doesn't exists
        /*
        mysql_create_db($mysql_db,$link);
        $sql = "Select * from RandomWords";
        $res = mysql_query ($sql);

        if(!$res){
                $sql = "CREATE TABLE RandomWords (item_id INT UNSIGNED not null AUTO_INCREMENT,item_nm VARCHAR (200),item_cd VARCHAR (15),PRIMARY KEY ( item_id ))";
                $res = mysql_query ($sql);
                populateDBRendom();
        }else{

		}
         * 
         */
	//populate db with 10000 records
	/*function populateDBRendom(){
		$filename = getcwd()."../../common/100000words.txt";
		$handle = fopen ($filename, "r");
		$contents = fread ($handle, filesize ($filename));
		$arWords = split(",",$contents);
		if(count($arWords)<2)
			$arWords = split("\n",$contents);
		//print(count($arWords));
		for($i=0;$i<count($arWords);$i++){
			$nm = $arWords[$i];
			$cd = rand(123456,987654);
			$sql = "INsert into RandomWords(item_nm,item_cd) Values('".$nm."','".$cd."')";
			mysql_query ($sql);
		}
		fclose ($handle);
	}*/
	getDataFromDB($_GET["mask"]);
	/*mysql_close($link);*/



	//print one level of the tree, based on parent_id
	function getDataFromDB($mask){
		$sqlstr = "SELECT localidad.loc_id, localidad.loc_nombre, departamento.dep_nombre, provincia.prov_nombre ".
                "FROM localidad, departamento, provincia ".
                "WHERE localidad.loc_dep_id = departamento.dep_id AND departamento.dep_prov_id = provincia.prov_id AND upper(trim(localidad.loc_nombre)) LIKE upper('".pg_escape_string(utf8_encode($mask))."%') ".
                "ORDER BY localidad.loc_nombre ASC, departamento.dep_nombre ASC, provincia.prov_nombre ASC LIMIT 50";
                $row = '';
                $conn = new Conexion();
                $consulta = $conn->retornar($sqlstr);

		if ( $_GET["pos"]==0)
			print("<complete>");
		else
			print("<complete add='true'>");
		if($consulta){
			while($row=pg_fetch_object($consulta)){
				print("<option value=\"".$row->loc_id."\">");
				print($row->loc_nombre.", ".$row->dep_nombre.", ".$row->prov_nombre);
				print("</option>");
			}
		}else{
			echo pg_errormessage($conn)." at ".__LINE__." line in ".__FILE__." file<br>";
		}
		print("</complete>");
	}
?>